package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DbConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.StringTokenizer;

import model.EmailingSystem;
import model.PollUserImpl;
import bean.Message;
import bean.PollUser;

public class DefinePollServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4572981338607696667L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		
		System.out.println("DefinePollServlet() run");
		
		// Get parameters
		String pollCreatorUsername = request.getParameter("poll_creator");
		String pollDefinition = request.getParameter("define_poll");
		
		// Error checking
		System.out.println("PollDefinition: "+pollDefinition);
		System.out.println("PollCreatorUsername: "+pollCreatorUsername);
		
		// Make message bean
		Message m = new Message();
		m.setText(pollDefinition);
		m.setSender(request.getSession().getAttribute("username").toString());
		
		// Error checking
		System.out.println("Message Text: "+m.getText());
		System.out.println("Message Sender Username: "+m.getSender());
		
		// Success tracking variable
		boolean error = false;
		
		// Add bean to database
		try {
			Connection conn = new DbConnection().getConn();
			String sql = "Insert into Message (Text, Sender) Values ('"+m.getText()+"','"+m.getSender()+"')";
			System.out.println("MESSAGE SQL: "+sql);
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.executeQuery();
		} catch (Exception e) {
			error = true;
			e.printStackTrace();
			System.out.println("SQL Problem in DefinePollServlet()");
		}
				
		// Get message id just created in db
		DbConnection db = new DbConnection();
		String sql2 = "select max(MessageId) from message";
		System.out.println("Select MessageId SQL: "+sql2);
		ResultSet rs = db.doSelect(sql2);
		int MessageId = 0;
		try {
			while (rs.next()) {
				
				MessageId = rs.getInt("MAX(MessageID)");
				System.out.println("Max Message ID: "+MessageId);	
			}
		} catch (Exception e) {
			error = true;
			System.out.println("SQL error in DefinePollServlet inside getMessageId section");
		}
		
		// Add receivers to User_Msg table
		String sql13 = "insert into User_Msg Values ('"+pollCreatorUsername+"',"+MessageId+")";
		System.out.println("Insert User_msg SQL: "+sql13);
		db.doInsert(sql13);
		
		// set session variable to success
		if (error) {
			request.getSession().setAttribute("define_poll_success", "f");
		} else {
			request.getSession().setAttribute("define_poll_success", "t");
		}
		
		// send redirect home
		response.sendRedirect("define_poll.jsp");
	}
}
